package com.cmc.cloud.cmclink.doc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.cmc.cloud.cmclink.csm.api.base.enums.DataTypeEnum;
import com.cmc.cloud.cmclink.doc.rpcservice.CustomerRpcService;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.AgentOperationPortErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.mapper.AgentOperationPortMapper;
import com.cmc.cloud.cmclink.doc.vo.agentoperationportvo.AgentOperationPortCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.agentoperationportvo.AgentOperationPortPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.agentoperationportvo.AgentOperationPortRespVO;
import com.cmc.cloud.cmclink.doc.vo.agentoperationportvo.AgentOperationPortUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;


import java.util.*;

import com.cmc.cloud.cmclink.doc.service.AgentOperationPortService;
import com.cmc.cloud.cmclink.doc.entity.AgentOperationPortDO;


/**
 * 单证代理操作口岸配置 Service 实现类
 *
 * @author 招商轮船-管理员
 */
@Service
@Validated
public class AgentOperationPortServiceImpl implements AgentOperationPortService {

    @Resource
    private AgentOperationPortMapper agentOperationPortMapper;

    @Resource
    private CustomerRpcService nameTransferService;

    @Override
    public void createAgentOperationPort(AgentOperationPortCreateReqVO createReqVO) {
        // 插入
        AgentOperationPortDO agentOperationPort = BeanUtil.copyProperties(createReqVO, AgentOperationPortDO.class);
        agentOperationPortMapper.insert(agentOperationPort);
    }

    @Override
    public void updateAgentOperationPort(AgentOperationPortUpdateReqVO updateReqVO) {
        // 校验存在
        validateAgentOperationPortExists(updateReqVO.getId());
        // 更新
        AgentOperationPortDO updateObj = BeanUtil.copyProperties(updateReqVO, AgentOperationPortDO.class);
        agentOperationPortMapper.updateById(updateObj);
    }

    @Override
    public void deleteAgentOperationPort(Long id) {
        // 校验存在
        validateAgentOperationPortExists(id);
        // 删除
        agentOperationPortMapper.deleteById(id);
    }

    private void validateAgentOperationPortExists(Long id) {
        if (agentOperationPortMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(AgentOperationPortErrorCodeConstants.AGENT_OPERATION_PORT_NOT_EXISTS);
        }
    }

    @Override
    public AgentOperationPortDO getAgentOperationPort(Long id) {
        return agentOperationPortMapper.selectById(id);
    }

    @Override
    public List<AgentOperationPortDO> getAgentOperationPortList(Collection<Long> ids) {
        return agentOperationPortMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<AgentOperationPortRespVO> getAgentOperationPortPage(AgentOperationPortPageReqVO reqVO) {
        LambdaQueryWrapperX<AgentOperationPortDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.eqIfPresent(AgentOperationPortDO::getOperationPortCode, reqVO.getOperationPortCode());
        if (StringUtils.isNotEmpty(reqVO.getKeyWord())) {
            List<String> strings = nameTransferService.addQuery(reqVO.getKeyWord(), DataTypeEnum.CUSTOMER);
            queryWrapperX.and(wrapper -> {
                wrapper.like(AgentOperationPortDO::getAgentCode, reqVO.getKeyWord());
                if (CollectionUtil.isNotEmpty(strings)) {
                    wrapper.or().in(AgentOperationPortDO::getAgentCode, strings);
                }
            });
        }
        PageResult<AgentOperationPortDO> pageResult = agentOperationPortMapper.selectPage(reqVO, queryWrapperX);
        List<AgentOperationPortRespVO> voList = BeanUtil.copyToList(pageResult.getList(), AgentOperationPortRespVO.class);
        voList.forEach(vo -> vo.setBkpCode(vo.getAgentCode()));
        if (CollectionUtil.isNotEmpty(voList)) {
            nameTransferService.fillName(voList);
        }
        return new PageResult<>(voList, pageResult.getTotal());
    }
}
